package com.red.wood.module.eval.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.eval.service.EvalReportInfoService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/evalReportInfo")
public class EvalReportInfoController {
	
	private static final Log log = LogFactory.getLog(EvalReportInfoController.class);
	
	@Autowired
	private EvalReportInfoService service;

	/**
	 **评测报告信息 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response){
		String type = request.getParameter("type");
		String userId = request.getParameter("userId");
		String evalNum = request.getParameter("evalNum");
		
		ModelAndView mv = new ModelAndView("eval_report/evalReportIdcNotInter");
		if(StringUtils.equals(type, "idcNotInter")) {
			mv = new ModelAndView("eval_report/evalReportIdcNotInter");
		}else if(StringUtils.equals(type, "idcOnlyInter")) {
			mv = new ModelAndView("eval_report/evalReportIdcOnlyInter");
		}else if(StringUtils.equals(type, "cdn")) {
			mv = new ModelAndView("eval_report/evalReportCdn");
		}else if(StringUtils.equals(type, "isp")) {
			mv = new ModelAndView("eval_report/evalReportIsp");
		}
		mv.addObject("userId",userId);
		mv.addObject("evalNum",evalNum);
		return mv;
	}
	
	
	@RequestMapping(value = "getIdcNotInterReport", method = RequestMethod.POST)
	@ResponseBody
	public String getIdcNotInterReport(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String evalNum = CommonUtils.valueOf(request.getParameter("evalNum"));
			String userId = CommonUtils.valueOf(request.getParameter("userId"));
			json.put("flag", true);
			json.putAll(service.getIdcNotInterReport(evalNum, userId));
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
			json.put("msg","生成报告异常！");
		}
		return json.toString();
	}
	
	@RequestMapping(value = "getIdcOnlyInterReport", method = RequestMethod.POST)
	@ResponseBody
	public String getIdcOnlyInterReport(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String evalNum = CommonUtils.valueOf(request.getParameter("evalNum"));
			String userId = CommonUtils.valueOf(request.getParameter("userId"));
			json.put("flag", true);
			json.putAll(service.getIdcOnlyInterReport(evalNum, userId));
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
			json.put("msg","生成报告异常！");
		}
		return json.toString();
	}
	
	@RequestMapping(value = "getCdnReport", method = RequestMethod.POST)
	@ResponseBody
	public String getCdnReport(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String evalNum = CommonUtils.valueOf(request.getParameter("evalNum"));
			String userId = CommonUtils.valueOf(request.getParameter("userId"));
			json.put("flag", true);
			json.putAll(service.getCdnReport(evalNum, userId));
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
			json.put("msg","生成报告异常！");
		}
		return json.toString();
	}
	
	@RequestMapping(value = "getIspReport", method = RequestMethod.POST)
	@ResponseBody
	public String getIspReport(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String evalNum = CommonUtils.valueOf(request.getParameter("evalNum"));
			String userId = CommonUtils.valueOf(request.getParameter("userId"));
			json.put("flag", true);
			json.putAll(service.getIspReport(evalNum, userId));
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
			json.put("msg","生成报告异常！");
		}
		return json.toString();
	}
}